//
//  MainHomeSCKBHomeViewCell.m
//  XTCloud
//
//  Created by QA on 2017/9/30.
//  Copyright © 2017年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "MainHomeSCKBHomeViewCell.h"

#import "FXCyclePagerView.h"
#import "FXPageControl.h"
#import "MainHomeSCKBViewCell.h"

@interface MainHomeSCKBHomeViewCell ()<FXCyclePagerViewDataSource, FXCyclePagerViewDelegate>

@property (nonatomic, strong) FXCyclePagerView *pagerView;
@property (nonatomic, strong) FXPageControl *pageControl;

@property (nonatomic, strong) NSMutableDictionary *viewCells;
@property (nonatomic, strong) UINavigationController *navigationController;

@property (nonatomic, strong) IBOutlet UIImageView *ktqxImageView;

@end

@implementation MainHomeSCKBHomeViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self initView];
        self.viewCells = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)initView
{
    FXCyclePagerView *pagerView = [[FXCyclePagerView alloc]initWithFrame:CGRectMake(0, 0, SuperWidth, 100)];
    pagerView.isInfiniteLoop = YES;
    pagerView.dataSource = self;
    pagerView.delegate = self;
    [pagerView registerClass:[MainHomeSCKBViewCell class] forCellWithReuseIdentifier:@"MainHomeSCKBViewCell"];
    [self.contentView addSubview:pagerView];
    self.pagerView = pagerView;
    
    FXPageControl *pageControl = [[FXPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_pagerView.frame) - 22, CGRectGetWidth(_pagerView.frame), 26)];
    pageControl.numberOfPages = 2;
    pageControl.currentPageIndicatorSize = CGSizeMake(8, 8);
    pageControl.currentPageIndicatorTintColor = ThemeColorBlue;
    pageControl.pageIndicatorTintColor = [UIColor colorWithWhite:0.85 alpha:1];
    [pagerView addSubview:pageControl];
    self.pageControl = pageControl;
}

-(void)setNavigation:(UINavigationController*)navigation
{
    self.navigationController = navigation;
    self.ktqxImageView.hidden = YES;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


#pragma mark - FXCyclePagerViewDataSource

- (NSInteger)numberOfItemsInPagerView:(FXCyclePagerView *)pageView {
    return 2;
}

- (UICollectionViewCell *)pagerView:(FXCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index
{
    NSString *mIdentifier = [NSString stringWithFormat:@"MainHomeSCKBViewCell-%ld",index];
    MainHomeSCKBViewCell *cell = [self.viewCells objectForKey:mIdentifier];
    if(!cell){
        cell = (MainHomeSCKBViewCell*)[pagerView dequeueReusableCellWithReuseIdentifier:@"MainHomeSCKBViewCell" forIndex:index];
        [cell setNavigation:self.navigationController index:index];
        [self.viewCells setObject:cell forKey:mIdentifier];
    }
    return cell;
}

- (FXCyclePagerViewLayout *)layoutForPagerView:(FXCyclePagerView *)pageView
{
    FXCyclePagerViewLayout *layout = [[FXCyclePagerViewLayout alloc] init];
    layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 100);
    layout.itemHorizontalCenter = YES;
    return layout;
}

- (void)pagerView:(FXCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    _pageControl.currentPage = toIndex;
    //[_pageControl setCurrentPage:newIndex animate:YES];
    NSLog(@"%ld ->  %ld",fromIndex,toIndex);
}

@end
